package network
import "net"
//import "fmt"
import util "protocol/util"


type NetSender struct{
	conn   *net.TCPConn
	buffer []byte
}

func NewSender(c *net.TCPConn, size int16)(s *NetSender){
	return &NetSender{c, make([]byte, size)}
}

func (s *NetSender) SendData(data []byte)(n int, err error) {
	//fmt.Println("send:", data)
	n, err = s.conn.Write(data)
	return
}

func (s *NetSender) SendMessage(msg util.Message)(n int, err error) {
	stream := util.NewByteStream(s.buffer)
	stream.MoveHead()
	msg.WriteToStream(stream)
	stream.WriteHead(msg.GetType())
	//fmt.Println("send:", stream.Data())
	n, err = s.conn.Write(stream.Data())
	return
}